﻿// import $config from '@/config'
import { ref, computed } from 'vue';
import { defineStore } from 'pinia';
import { removeStore } from '@/utils/utils';

const USERINFO = 'mamak_userInfo';
const CONFIG = 'mamak_config';

export const useUserStore = defineStore(
    'user',
    () => {
        const token = ref('');
        const authList = ref<string[]>();
        const userInfo = ref<Record<string, any>>();

        const setUserInfo = (val: Record<string, any>) => {
            userInfo.value = val;
        };
        const setToken = (val: string) => {
            token.value = val;
        };

        const isLogin = computed(() => {
            return !!token.value;
        });

        const logOut = () => {
            removeStore(USERINFO);
            removeStore(CONFIG);
        };

        const setAuthList = (val: string[]) => {
            authList.value = val;
        };

        return { userInfo, token, setUserInfo, setToken, isLogin, logOut, authList, setAuthList };
    },
    {
        persist: {
            key: 'mamak_userInfo',
            storage: sessionStorage,
        },
    }
);
